//
//  HWCloudGamePagerCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/14.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import FSPagerView
class HWCloudGamePagerCell:FSPagerViewCell {
    var bgView:UIView!
    var topTagImageView:UIImageView!
    var gameIconView:UIImageView!
    var nameLabel:UILabel!
    var textTagCollectionView:TTGTextTagCollectionView!
    var contentTitleImage:UIImageView!
    var contentLabel:UILabel!
    var playBgView:UIView!
    var playImage:UIImageView!
    var playTitle:UILabel!
    var playButton:UIButton!
    var downBgView:UIView!
    var downImage:UIImageView!
    var downTitle:UILabel!
    var downButton:UIButton!
    var bottomBgView:UIView!
    var bottomTagImage:UIImageView!
    var bottomTitleLabel:UILabel!
    var bottomTipsLabel:UILabel!
    
    var model:HWCloudGameModel? {
        didSet {
            gameIconView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            topTagImageView.kf.setImage(with: URL(string: model?.hot ?? ""))
            nameLabel.text = model?.name
            if model?.tishi.count ?? 0 > 0 {
                bottomBgView.isHidden = false
                bottomTipsLabel.text = model?.tishi
            } else {
                bottomBgView.isHidden = true
            }
            contentLabel.text = model?.content
            
            if model?.style == "1" {
                playTitle.text = "去玩"
            } else {
                playTitle.text = "已占用"
            }
            textTagCollectionView.removeAllTags()
            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 11*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 10*widthScale
                normalStyle.extraSpace = CGSize.init(width: 14*widthScale, height: 4*widthScale)
                normalStyle.backgroundColor = .clear
                normalStyle.shadowColor = .clear
                if idx == 0 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FB9C0C")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#FB9C0C")
                }else if idx == 1 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#1CD67E")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#1CD67E")
                }else if idx == 2 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FF726A")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#FF726A")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#444444")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#444444")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
            }
        }
    }
    
    var playButtonClick:((_:HWCloudGameModel)->())?
    var downButtonClick:((_:HWCloudGameModel)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        
        contentView.layer.shadowColor = UIColor.clear.cgColor
        
        bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 0, width: 322*widthScale, height: 598*widthScale)
        bgView.layer.cornerRadius = 20*widthScale
        bgView.layer.masksToBounds = true
        contentView.addSubview(bgView)
        
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithAlphaHexString(hexString: "#174843", alpha: 0.7).cgColor,
                                                     UIColor.colorWithAlphaHexString(hexString: "#071C19", alpha: 0.47).cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 1))
        
        topTagImageView = UIImageView()
        topTagImageView.layer.masksToBounds = true
        topTagImageView.backgroundColor = .clear
        topTagImageView.contentMode = .scaleAspectFill
        contentView.addSubview(topTagImageView)
        
        gameIconView = UIImageView()
        gameIconView.layer.cornerRadius = 15*widthScale
        gameIconView.layer.masksToBounds = true
        gameIconView.backgroundColor = .clear
        gameIconView.contentMode = .scaleAspectFill
        bgView.addSubview(gameIconView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 24*widthScale)
        bgView.addSubview(nameLabel)

        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self;
        textTagCollectionView.alignment = TTGTagCollectionAlignment.center;
        textTagCollectionView.manualCalculateHeight = true;
        bgView.addSubview(textTagCollectionView)
        
        contentTitleImage = UIImageView()
        contentTitleImage.image = UIImage(named: "cloudGameContentTitle")
        bgView.addSubview(contentTitleImage)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#EEEEEE", font: 13*widthScale)
        bgView.addSubview(contentLabel)
        
        playBgView = UIView()
        playBgView.backgroundColor = .colorWithHexString(hexString: "#1CD67E")
        playBgView.layer.cornerRadius = 24*widthScale
        bgView.addSubview(playBgView)
        
        playImage = UIImageView()
        playImage.image = UIImage(named: "cloudGamePlay")
        playBgView.addSubview(playImage)
        
        playTitle = HWLabel.createBoldLabel(title: "云玩", color: "#FFFFFF", font: 14*widthScale)
        playBgView.addSubview(playTitle)
        
        playButton = UIButton(type: .custom)
        playButton.addTarget(self, action: #selector(playButtonClick(_:)), for: .touchUpInside)
        playBgView.addSubview(playButton)
        
        downBgView = UIView()
        downBgView.layer.cornerRadius = 24*widthScale
        downBgView.frame = CGRect(x: 0, y: 0, width: 116*widthScale, height: 48*widthScale)
        downBgView.layer.masksToBounds = true
        bgView.addSubview(downBgView)
        UIColor.gradientColor(view: downBgView, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                         UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        
        downImage = UIImageView()
        downImage.image = UIImage(named: "cloudGameDownImage")
        downBgView.addSubview(downImage)
        
        downTitle = HWLabel.createBoldLabel(title: "下载", color: "#FFFFFF", font: 14*widthScale)
        downBgView.addSubview(downTitle)
        
        downButton = UIButton(type: .custom)
        downButton.addTarget(self, action: #selector(downButtonClick(_:)), for: .touchUpInside)
        downBgView.addSubview(downButton)
        
        bottomBgView = UIView()
        bottomBgView.layer.cornerRadius = 30*widthScale
        bottomBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#FF726A", alpha: 0.26)
        bgView.addSubview(bottomBgView)
        
        bottomTagImage = UIImageView()
        bottomTagImage.image = UIImage(named: "cloudGameTip")
        bottomBgView.addSubview(bottomTagImage)
        
        bottomTitleLabel = HWLabel.createBoldLabel(title: "提示", color: "#FF726A", font: 14*widthScale)
        bottomBgView.addSubview(bottomTitleLabel)
        
        bottomTipsLabel = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 12*widthScale)
        bottomBgView.addSubview(bottomTipsLabel)
    }
    
    @objc func playButtonClick(_ button:UIButton) -> Void {
        self.playButtonClick?(self.model ?? HWCloudGameModel())
    }
    
    @objc func downButtonClick(_ button:UIButton) -> Void {
        self.downButtonClick?(self.model ?? HWCloudGameModel())
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(7*widthScale)
        }
        
        topTagImageView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(33*widthScale)
            make.width.equalTo(54*widthScale)
            make.height.equalTo(37*widthScale)
        }
        
        
        gameIconView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(36*widthScale)
            make.width.height.equalTo(91*widthScale)
        }
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(gameIconView.snp.bottom).offset(14*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20*widthScale)
            make.top.equalTo(nameLabel.snp.bottom).offset(9*widthScale)
        }
        
        contentTitleImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(textTagCollectionView.snp.bottom).offset(16*widthScale)
            make.width.equalTo(58*widthScale)
            make.height.equalTo(23*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(contentTitleImage.snp.bottom).offset(9*widthScale)
            make.height.equalTo(145*widthScale)
        }
        
        playBgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(contentLabel.snp.bottom).offset(33*widthScale)
            make.width.equalTo(159*widthScale)
            make.height.equalTo(49*widthScale)
        }
        
        playImage.snp.makeConstraints { make in
            make.left.equalTo(53*widthScale)
            make.centerY.equalToSuperview()
            make.height.width.equalTo(18*widthScale)
        }
        
        playTitle.snp.makeConstraints { make in
            make.left.equalTo(playImage.snp.right).offset(7*widthScale)
            make.centerY.equalToSuperview()
        }
        
        playButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        downBgView.snp.makeConstraints { make in
            make.left.equalTo(playBgView.snp.right).offset(11*widthScale)
            make.top.equalTo(playBgView)
            make.height.equalTo(48*widthScale)
            make.width.equalTo(116*widthScale)
        }
        
        downImage.snp.makeConstraints { make in
            make.left.equalTo(28*widthScale)
            make.centerY.equalToSuperview()
            make.height.width.equalTo(18*widthScale)
        }
        
        downTitle.snp.makeConstraints { make in
            make.left.equalTo(downImage.snp.right).offset(7*widthScale)
            make.centerY.equalToSuperview()
        }
        
        downButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bottomBgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.height.equalTo(87*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(downBgView.snp.bottom).offset(16*widthScale)
        }
        
        bottomTagImage.snp.makeConstraints { make in
            make.left.equalTo(119*widthScale)
            make.top.equalTo(17*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        bottomTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(bottomTagImage.snp.right).offset(6*widthScale)
            make.centerY.equalTo(bottomTagImage)
        }
        
        bottomTipsLabel.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(bottomTagImage.snp.bottom).offset(8*widthScale)
            make.height.equalTo(33*widthScale)
            make.right.equalTo(-20*widthScale)
        }
        
    }

}

extension HWCloudGamePagerCell:TTGTextTagCollectionViewDelegate{
    
    @nonobjc func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {

        
    }
    
    
}
